#include <locale.h>

#include "common.h"

int
main(int argc, char **argv)
{
	GError *err = NULL;
	GMainLoop *loop;
	int a;
	gchar *c;

	CopticWindow *mainwin;

	loop = g_main_loop_new(NULL, FALSE);

	/*
	 * Set locale
	 */

	if (!setlocale(LC_ALL, "")) {
		g_printerr("Can't set the specified locale!\n");
		return 1;
	}

	/*
	 * Read configuration file (and act accordingly)
	 */

	if (keyfile_read() == FALSE)
		return 1;

	a = g_key_file_get_integer(keyfile_get(), "NCurses Interface Core",
				   "contact_list_width", &err);
	if (err != NULL) {
		if (err->code == G_KEY_FILE_ERROR_INVALID_VALUE)
			g_printerr("cannot get contact_list_width: %s",
				   err->message);

		g_clear_error(&err);
	} else {
		set_contact_list_width(a);
	}

	set_interface_encoding(g_strdup("UTF-8"));
	c = g_key_file_get_string(keyfile_get(), "NCurses Interface Core",
				  "codeset", &err);
	if (err != NULL) {
		if (err->code == G_KEY_FILE_ERROR_INVALID_VALUE)
			g_printerr("cannot get character codeset: %s",
				   err->message);

		g_clear_error(&err);
	} else {
		set_interface_encoding(c);
	}

	/*
	 * Initialize the module registration routine
	 */

	regproto_init();

	/*
	 * Initialize the interface
	 */

	init_interface();

	/*
	 * Call redraw_interface on SIGWINCH 
	 */

	setup_signal_handler();

	/*
	 * Draw the interface
	 */

	draw_interface();

	/*
	 * And finally... Run the main-loop.
	 */

	mainwin->ncwin = newwin(20, 50, 2, 2);
	box(mainwin->ncwin, 0, 0);
	make_topwin(mainwin);
	text_parser_insert(mainwin, "<t>Jonas | \xc3\xa4\xE4\xB8\xB4"
			   "\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4"
			   "\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4"
			   "\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4"
			   "\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4"
			   "\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4\xE4\xB8\xB4"
			   "<b>and this text should be bold</b> but not this.. I wonder if this is wrapping</t>");

	g_main_run(loop);
	g_main_destroy(loop);

	return 0;
}
